<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">





  <link rel="alternate" href="/atom.xml" title="renhao" type="application/atom+xml">






<meta name="description" content="相关知识点 ：normal flow（一般流）、containing block（包含块）、bfc（块级可视化上下文）、margin collapse（外边距合并）、base line（基线）、writing mode（书写模式）、bidi（文本方向）">
<meta property="og:type" content="article">
<meta property="og:title" content="css盒模型">
<meta property="og:url" content="https://huangrenhao.gitee.io/2019/03/08/css-box-model/index.html">
<meta property="og:site_name" content="renhao">
<meta property="og:description" content="相关知识点 ：normal flow（一般流）、containing block（包含块）、bfc（块级可视化上下文）、margin collapse（外边距合并）、base line（基线）、writing mode（书写模式）、bidi（文本方向）">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://huangrenhao.gitee.io/2019/03/08/css-box-model/基本框.jpg">
<meta property="og:image" content="https://huangrenhao.gitee.io/2019/03/08/css-box-model/css-box-model.png">
<meta property="og:updated_time" content="2020-09-03T08:50:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="css盒模型">
<meta name="twitter:description" content="相关知识点 ：normal flow（一般流）、containing block（包含块）、bfc（块级可视化上下文）、margin collapse（外边距合并）、base line（基线）、writing mode（书写模式）、bidi（文本方向）">
<meta name="twitter:image" content="https://huangrenhao.gitee.io/2019/03/08/css-box-model/基本框.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://huangrenhao.gitee.io/2019/03/08/css-box-model/">





  <title>css盒模型 | renhao</title>
  








<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">renhao</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://huangrenhao.gitee.io/2019/03/08/css-box-model/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="renhao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="renhao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">css盒模型</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-08T20:22:40+08:00">
                2019-03-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/css/" itemprop="url" rel="index">
                    <span itemprop="name">css</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/03/08/css-box-model/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/03/08/css-box-model/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.4k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>相关知识点 ：normal flow（一般流）、containing block（包含块）、bfc（块级可视化上下文）、margin collapse（外边距合并）、base line（基线）、writing mode（书写模式）、bidi（文本方向）<a id="more"></a></p>
<h3 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h3><p>在了解布局之前首先得分析一下要展示的内容。CSS假定每个元素都有一个基本框，一般都是一个矩形框，每个元素框中心有一个内容区，内容区有内边距，外边距，边框等几个属性。基本框也可以可以看做一个盒子，盒模型。指定一个元素的高度和宽度属性时只是设置内容区域的宽度和高度，完全大小的元素必须添加填充、边框和边距。</p>
<p><img src="/2019/03/08/css-box-model/基本框.jpg" alt="基本框"></p>
<h4 id="盒模型"><a href="#盒模型" class="headerlink" title="盒模型"></a>盒模型</h4><p>盒模型可以分为IE盒模型和W3C标准盒模型，两者的区别在于标准盒模型中属性width和height只包含内容content，不包含 border和 padding，而 IE 盒模型属性width和height包含border和padding，即是content+padding+border。两种盒模型可以由CSS属性box-sizing控制。（box-sizing为content-box，标准盒模型，也为默认值；box-sizing为border-box，IE盒模型）盒模型有一个需要注意的点就是盒模型的高宽并不包括margin，虽然盒模型包括content、padding、border、margin，但盒子的大小由content+padding+border，margin算进来只是盒子占据的位置，不是盒子的大小。</p>
<p><img src="/2019/03/08/css-box-model/css-box-model.png" alt="css 盒模型" style="zoom:50%;"></p>
<h4 id="盒的基本类型"><a href="#盒的基本类型" class="headerlink" title="盒的基本类型"></a>盒的基本类型</h4><ul>
<li><p>块级盒子：<code>display:block</code></p>
</li>
<li><p>行内块级盒子：<code>display:inline-block</code></p>
</li>
<li><p>行内盒子：<code>display:inline</code></p>
</li>
</ul>
<p>使用display属性来定义盒的基本类型，可以分为block类型和inline类型等等。div、p等元素代表的block类型元素和宽度占满了整个浏览器的宽度（无论宽度是否指定，宽度指定的从上面介绍的可以看出），span、a元素所代表的inline类型的元素的宽度只等于其内容所在的宽度，每一行中只允许一个block类型的元素，但是可以并列容纳多个inline类型的元素。inline-block类型属于block类型盒的一种，但是在显示时具有inline类型盒的特点，采用这点的原因可能是inline类型的元素不支持设置宽度高度，元素的宽高完全由content、border和padding的大小撑开。</p>
<h3 id="inline-boxes"><a href="#inline-boxes" class="headerlink" title="inline boxes"></a>inline boxes</h3><p>行级盒子是在同一行排列的，排在同一行的这些盒子都是inline boxes。由标签生成的inline-block和inline盒子是有名字的inline boxes，而文字则属于匿名的inline boxes。</p>
<h3 id="line-boxes模型"><a href="#line-boxes模型" class="headerlink" title="line boxes模型"></a>line boxes模型</h3><p>line boxes就是代表包含众多inline boxes的这行。line boxes的高度等于这行行内高度最大的inline boxes的高度。</p>
<p>实例：<code>&lt;p&gt;这是一段普通的文字，这里有个&lt;em&gt;em&lt;/em&gt;标签&lt;/p&gt;</code></p>
<p>这里涉及四种boxes，首先是<code>&lt;p&gt;</code>标签所在的containing box，此box包含了其他的boxes；然后就是inline boxes，<code>这是一段普通的文字，这里有个</code>和<code>标签</code>等只有文字的为匿名inline boxes，<code>em</code>等含inline属性的标签（<code>&lt;span&gt;</code>、<code>&lt;a&gt;</code>、<code>&lt;cite&gt;</code>）为inline boxes。content area是一种围绕文字看不见的box，content area的大小同font-size大小相关。故默认情况下line boxes模型中的图片inline boxes与同一line boxes中的inline boxes对齐，即一张图片只能与一行文字对齐，要让一张图片与多行文字对齐就需要破坏正常的line boxes模型：在图片上加入float属性后图片的inline boxes就不存在了，图片就会从line boxes上脱离出来，跟随自身的方位属性靠边排列。</p>
<h3 id="一般流"><a href="#一般流" class="headerlink" title="一般流"></a>一般流</h3><p>一般流的理解就是将屏幕中显示的内容都可以对应为文档中的一个元素，有内联元素（行内元素）和块级元素，</p>
<h3 id="包含块"><a href="#包含块" class="headerlink" title="包含块"></a>包含块</h3><p>每个元素都是相对于其他包含块摆放，包含块就是一个元素的布局上下文，每个元素的包含块由最近的块级元素担当。</p>
<h3 id="边距合并"><a href="#边距合并" class="headerlink" title="边距合并"></a>边距合并</h3><blockquote>
<p>遇到的问题：</p>
<ul>
<li><p>当父级元素没有设置border-top或者padding-top的时候，他的第一个子节点（这里包括<strong>元素节点</strong>和<strong>文本节点</strong>）如果设置了margin-top，那么这个margin-top会一层层往上越级直到遇到</p>
<ul>
<li>(1)一个设置了border-top或者padding-top的父元素</li>
<li>(2)直到遇到body</li>
<li>(3)此父级元素的第一个子节点不为他或他的父元素的时候才停下。</li>
</ul>
<p>解决方法：</p>
<ul>
<li>给父元素设置一个border-top或者padding-top把子元素管住就可以防止越级了</li>
<li>可以让此子元素不属于流内块级盒（浮动、inline-block、绝对定位），这里也是将子元素设置 bfc（<strong>但设置 overflow 不为 visible 不能让几个 bfc 的外边距摆脱合并？</strong>）</li>
<li>给父元素设置 bfc，让父元素不和它的子元素发生 margin 折叠，即父元素里面的元素不影响外面的布局</li>
</ul>
</li>
<li><p>元素自身的 margin-bottom 和 margin-top 相邻时也会折叠（元素自身高度为 0）</p>
</li>
</ul>
</blockquote>
<h3 id="position属性常用的取值和它们的基本行为"><a href="#position属性常用的取值和它们的基本行为" class="headerlink" title="position属性常用的取值和它们的基本行为"></a>position属性常用的取值和它们的基本行为</h3><ul>
<li><p>static</p>
<p>static是position属性的默认值，定位方式为静态文档流。</p>
</li>
<li><p>relative：对文档流位置偏移</p>
</li>
<li><p>absolute：根据第一个非 static 的父节点进行定位，如果找不到非 static 的父元素应该是根据 body 定位。</p>
</li>
<li><p>fixed：根据 viewport 定位</p>
</li>
<li><p>sticky：static+fixed 的组合。粘性定位，需要配合 top、bottom、left、right 中的任意一种，只有达到指定的阈值才会被固定。</p>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/01/22/js-shen-kao-bei-qian-kao-bei/" rel="next" title="【转载】js深拷贝和浅拷贝">
                <i class="fa fa-chevron-left"></i> 【转载】js深拷贝和浅拷贝
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/03/26/react-history/" rel="prev" title="React中的BrowserRouter与HashRouter">
                React中的BrowserRouter与HashRouter <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">renhao</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">94</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#基础"><span class="nav-number">1.</span> <span class="nav-text">基础</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#盒模型"><span class="nav-number">1.1.</span> <span class="nav-text">盒模型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#盒的基本类型"><span class="nav-number">1.2.</span> <span class="nav-text">盒的基本类型</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#inline-boxes"><span class="nav-number">2.</span> <span class="nav-text">inline boxes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#line-boxes模型"><span class="nav-number">3.</span> <span class="nav-text">line boxes模型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一般流"><span class="nav-number">4.</span> <span class="nav-text">一般流</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#包含块"><span class="nav-number">5.</span> <span class="nav-text">包含块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#边距合并"><span class="nav-number">6.</span> <span class="nav-text">边距合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#position属性常用的取值和它们的基本行为"><span class="nav-number">7.</span> <span class="nav-text">position属性常用的取值和它们的基本行为</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">renhao</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">265.9k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '2P1xqUWiCoNm14MH4yhURlJi-gzGzoHsz',
        appKey: 'uJN0uagHIVgXtBO1OuLV9Ban',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
